import {createWebHashHistory, createRouter, RouteLocationNormalized, NavigationGuardNext} from "vue-router"
import basicRoute from "../config/route";
import store from '../store'
import {searchLanguage} from "../api/LanguageRequest";
import {searchTypeAllAsTree} from "../api/GoodTypeRequest";
import {getUserInfo} from "../api/UserActionRequest";
import {searchCart} from "../api/CartRequest";
import {addIpAddress} from "../api/IpAddressRequest";
import {ref} from "vue";
import {searchMainPageLast} from "../api/MainPageRequest";
import {searchCompany} from "../api/CompanyRequest";
import {searchGoodById} from "../api/GoodRequest";

const router = createRouter({
    history: createWebHashHistory(),
    routes: basicRoute
})

const isAddIp = ref(false)

router.beforeEach(async (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {


    if (!isAddIp.value) {
        addIpAddress({url: to.path, source: document.referrer})
        isAddIp.value = true
    }


    // @ts-ignore
    if (store.state.globalInfo.languageMap.size == 0) {
         searchLanguage().then((res) => {
            store.commit('SET_GLOBAL_INFO', {
                languageList: res.data.result
            })
        })
    }

    if (store.state.globalInfo.mainPageInfo == null) {
        searchMainPageLast().then(async (res) => {

            // @ts-ignore
            if (res.data.result.exampleGoodsId != null && res.data.result.exampleGoodsId.trim().length > 0) {
                // @ts-ignore
                await searchGoodById(res.data.result.exampleGoodsId.trim()).then((res1)=>{
                    // @ts-ignore
                    res.data.result.exampleGoods=res1.data.result
                }).catch(()=>{})
            }

            // @ts-ignore
            res.data.result.leftText = JSON.parse(res.data.result.leftText)
            // @ts-ignore
            res.data.result.mainSlideshows = JSON.parse(res.data.result.mainSlideshows)
            // @ts-ignore
            res.data.result.rightText = JSON.parse(res.data.result.rightText)
            // @ts-ignore
            res.data.result.selfDefineModules = JSON.parse(res.data.result.selfDefineModules)
            // @ts-ignore
            res.data.result.secondaryImages = JSON.parse(res.data.result.secondaryImages)
            // @ts-ignore
            res.data.result.sideImages = JSON.parse(res.data.result.sideImages)
            store.commit('SET_GLOBAL_INFO', {
                mainPageInfo: res.data.result
            })
        })
    }
    if (store.state.globalInfo.companyInfo == null) {
        searchCompany().then((res) => {
            // @ts-ignore
            res.data.result.info = JSON.parse(res.data.result.info)
            store.commit('SET_GLOBAL_INFO', {

                companyInfo: res.data.result
            })
        })
    }


    // @ts-ignore
    if (store.state.globalInfo.goodTypeTreeList.length == 0) {
        await searchTypeAllAsTree().then((res) => {
            store.commit('SET_GLOBAL_INFO', {
                goodTypeTreeList: res.data.result
            })
        })
    }

    if (store.state.config.token != null && store.state.config.token != '' && store.state.globalInfo.userInfo == null) {
         getUserInfo().then((res) => {
            store.commit('SET_GLOBAL_INFO', {
                userInfo: res.data.result
            })
            searchCart().then((res) => {
                if (res.data.result != undefined && res.data.result.cartList != null) {
                    for (let i = 0; i < res.data.result.cartList.length; i++) {
                        //@ts-ignored
                        res.data.result.cartList[i].good.specification.specification = JSON.parse(res.data.result.cartList[i].good.specification.specification)
                    }
                }
                store.commit('SET_GLOBAL_INFO', {cartInfo: res.data.result})
            })
        })
    }

    if (store.state.config.token == null || store.state.config.token == '') {
        if (to.path.startsWith('/user')) {

            window.scrollTo(0,0);
            next({
                name: 'Auth'
            })
            return;
        }
    }

    window.scrollTo(0,0);
    next();
})

export default router;

